package com.youmans.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youmans.commonutils.R;
import com.youmans.eduservice.entity.EduCourse;
import com.youmans.eduservice.entity.EduTeacher;
import com.youmans.eduservice.entity.vo.CourseInfoVo;
import com.youmans.eduservice.entity.vo.CoursePublishVo;
import com.youmans.eduservice.entity.vo.CourseQuery;
import com.youmans.eduservice.service.EduCourseService;
import com.youmans.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Youmans
 * @since 2022-05-07
 */
@RestController
@RequestMapping("/eduservice/course")
@Api(tags = "课程控制器")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduTeacherService eduTeacherService ;
    @ApiOperation("添加课程基本信息的方法")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后课程id，为后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo) ;
        return R.ok().data("courseId",id) ;
    }

    @ApiOperation("根据课程id查询课程基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo vo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",vo) ;
    }

    @ApiOperation("修改课程信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo) ;
        return R.ok() ;
    }
    @ApiOperation("根据课程id查询课程确认信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo vo = courseService.getPublishCourseInfo(id) ;
        return R.ok().data("publishCourse",vo) ;
    }
    @ApiOperation("课程最终发布，修改课程状态")
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id) ;
        eduCourse.setStatus("Normal") ;
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //完善条件查询带分页
    @ApiOperation("获取全部的课程")
    @GetMapping("getCourseList")
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null) ;
        return R.ok().data("list",list) ;
    }

//    @Cacheable(value = "course",key = "'selectCoursePage'")
    @ApiOperation("分页查询课程带条件")
    @PostMapping("getCourseList/{pageNum}/{pageSize}")//required = false表示参数可以为空
    public R getCourseList(
            @ApiParam(value = "分页查询的页码数",defaultValue = "1")@PathVariable("pageNum") Integer pageNum,
            @ApiParam(value = "分页查询的记录数",defaultValue = "3")@PathVariable("pageSize") Integer pageSize,
            @ApiParam("查询条件")@RequestBody(required = false) CourseQuery courseQuery
    ){
        //        分页带条件查询
        Page<EduCourse> pageCourse = new Page<>(pageNum, pageSize);
        IPage<EduCourse> gradeIPage = null ;
        if(!StringUtils.isEmpty(courseQuery)){
            //        通过服务层
            gradeIPage = courseService.pageCondition(pageCourse,courseQuery);
        }else{
            QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("gmt_create");
            gradeIPage = courseService.page(pageCourse,wrapper);
        }


        long total = gradeIPage.getTotal();//总记录数
        List<EduCourse> records = gradeIPage.getRecords();//数据list集合
//        封装result对象并返回
        LinkedHashMap map = new LinkedHashMap() ;


        //将所有讲师查询出来，放前端进行显示
        List<EduTeacher> list = eduTeacherService.list(null);
        map.put("teacherList",list);

        records.stream().forEach(
                m->{
                    for (EduTeacher eduTeacher : list) {
                        if(m.getTeacherId().equals(eduTeacher.getId())){
                            m.setTeacher(eduTeacher.getName()) ;
                        }
                    }
                }
        );

        map.put("total",total) ;
        map.put("rows",records) ;

        return R.ok().data(map) ;
//        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("删除课程")
    @DeleteMapping("removeCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId)  ;
        return R.ok() ;
    }
}

